package com.example.rxjavautildemo.base;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.App;
import com.squareup.leakcanary.RefWatcher;
import com.xuexiang.rxutil2.lifecycle.RxLifecycle;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseActivity extends AppCompatActivity {
    private Unbinder mUnbinder;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        RxLifecycle.injectRxLifecycle(this);
        mUnbinder = ButterKnife.bind(this);
        initArgs();
        initViews();
        initListener();
    }

    protected void initArgs(){

    }
    /**
     * 布局的资源id
     * @return
     */
    @LayoutRes
    protected abstract int getLayoutId();

    /**
     * 初始化控件
     */
    protected abstract void initViews();

    /**
     * 初始化监听器
     */
    protected abstract void initListener();

    protected <T extends View> T $(int id){
        return (T) findViewById(id);
    }

    protected void startActivity(Class<? extends Activity> clazz){
        startActivity(new Intent(this,clazz));
    }

    protected void toast(String msg){
        Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        mUnbinder.unbind();
        super.onDestroy();
        RefWatcher refWatcher = App.getRefWatcher();
        refWatcher.watch(this);
    }
}
